package com.pd.kit;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * DateKit.
 */
public class DateKit {

	public static String datePattern = "yyyy-MM-dd";
	public static String timeStampPattern = "yyyy-MM-dd HH:mm:ss";
	public static String unionTimeStampPattern = "YYYYMMddHHmmss";
	public static String UnionDateStampPattern = "MMdd";

	/**
     * 格式话默认模式
	 * @param datePattern
	 */
	public static void setDatePattern(String datePattern) {
		if (StringUtils.isBlank(datePattern)) {
			throw new IllegalArgumentException("datePattern can not be blank");
		}
		DateKit.datePattern = datePattern;
	}

	/**
     * 设置时间戳模式
	 * @param timeStampPattern
	 */
	public static void setTimeStampPattern(String timeStampPattern) {
		if (StringUtils.isBlank(timeStampPattern)) {
			throw new IllegalArgumentException("timeStampPattern can not be blank");
		}
		DateKit.timeStampPattern = timeStampPattern;
	}

	/**
     * 转换Date类型
	 * @param dateStr
     * @return
     */
	public static Date toDate(String dateStr) {
		if (StringUtils.isBlank(dateStr)) {
			return null;
		}

		dateStr = dateStr.trim();
		int length = dateStr.length();
		try {
			if (length == timeStampPattern.length()) {
				SimpleDateFormat sdf = new SimpleDateFormat(timeStampPattern);
				try {
					return sdf.parse(dateStr);
				} catch (ParseException e) {
					dateStr = dateStr.replace(".", "-");
					dateStr = dateStr.replace("/", "-");
					return sdf.parse(dateStr);
				}
			} else if (length == datePattern.length()) {
				SimpleDateFormat sdfDate = new SimpleDateFormat(datePattern);
				try {
					return sdfDate.parse(dateStr);
				} catch (ParseException e) {
					dateStr = dateStr.replace(".", "-");
					dateStr = dateStr.replace("/", "-");
					return sdfDate.parse(dateStr);
				}
			} else if (length == unionTimeStampPattern.length()) {
				SimpleDateFormat sdfDate = new SimpleDateFormat(unionTimeStampPattern);
				try {
					return sdfDate.parse(dateStr);
				} catch (ParseException e) {
					dateStr = dateStr.replace(".", "-");
					dateStr = dateStr.replace("/", "-");
					return sdfDate.parse(dateStr);
				}
			} else {
				throw new IllegalArgumentException("The date format is not supported for the time being");
			}
		} catch (ParseException e) {
			throw new IllegalArgumentException("The date format is not supported for the time being");
		}
	}

	/**
     * 转换成指定格式内容
	 * @param date
     * @return
     */
	public static String toStr(Date date) {
		return toStr(date, DateKit.datePattern);
	}

	public static String toStr(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	/**
	 * 当前时间延后一段时间
	 * @param date
	 * @param time
	 * @param pattern
	 * @return
	 */
	public static String toStr(Date date,long time, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date.getTime()+time);
	}

	public static String getDateShortStartOrEnd(DayFlag dayFlag) {
		return getDateTimeStartOrEnd(dayFlag, false);
	}

	public static String getDateStartOrEnd(DayFlag dayFlag) {
		return getDateTimeStartOrEnd(dayFlag, true);
	}

	/**
	 * 获取今天，昨天的开始日期，结束日期   YESTERDAY_START,YESTERDAY_END,TODAY_START,TODAY_END
	 * @param dayFlag
	 * @return
	 */
	public static String getDateTimeStartOrEnd(DayFlag dayFlag,Boolean fullTime) {
		String dateStr = "";
		Calendar calendar = Calendar.getInstance();
		switch (dayFlag) {
			case TODAY_START : {
				dateStr = toStr(calendar.getTime(),datePattern);
				dateStr = fullTime ? (dateStr + " 00:00:00") : dateStr;
				break;
			}
			case TODAY_END : {
				dateStr = toStr(calendar.getTime(),datePattern);
				dateStr = fullTime ? (dateStr + " 23:59:59") : dateStr;
				break;
			}
			case YESTERDAY_START : {
				calendar.add(Calendar.DATE,-1);
				dateStr = toStr(calendar.getTime(),datePattern);
				dateStr = fullTime ? (dateStr + " 00:00:00") : dateStr;
				break;
			}
			case YESTERDAY_END : {
				calendar.add(Calendar.DATE,-1);
				dateStr = toStr(calendar.getTime(),datePattern);
				dateStr = fullTime ? (dateStr + " 23:59:59") : dateStr;
				break;
			}
			case MONTH_THIS_START : {
				calendar.set(Calendar.DATE, 1);
				dateStr = toStr(calendar.getTime(),datePattern);
				dateStr = fullTime ? (dateStr + " 00:00:00") : dateStr;
				break;
			}
			case MONTH_THIS_END : {
				calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
				dateStr = toStr(calendar.getTime(),datePattern);
				dateStr = fullTime ? (dateStr + " 23:59:59") :	dateStr;
				break;
			}

		}

		return dateStr;
	}

	enum DayFlag {
		YESTERDAY_START,YESTERDAY_END,
		TODAY_START,TODAY_END,
		MONTH_THIS_START,MONTH_THIS_END
	}

	public static void main(String[] args) {
		System.out.println(toStr(new Date(), UnionDateStampPattern));
	}
}


